package com.see.client;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.see.domain.MessegeParser;
import com.see.domain.Protocol;
import com.see.exception.MessegeFeedException;
import com.see.exception.UserExistException;

public class LoginListner implements Listener {
	
	private TradingClientImpl tradingClient;
	private volatile boolean loginSuccsess = false;
	private volatile String listnerResult;
	private CountDownLatch latch;

	public LoginListner(TradingClientImpl tradingClient) {
		this.tradingClient = tradingClient;
		latch = new CountDownLatch(1);
	}
	

	@Override
	public synchronized void notificate(HashMap<String, String> paramMap){
		String answer = "";
		String cmd = paramMap.get(MessegeParser.MsgFields.TYPEMSG.name());
		if(cmd.equals(Protocol.LOGIN_FIELD)){
			answer = paramMap.get(MessegeParser.MsgFields.SUCCESS.name());
			
			if (answer.equals(Protocol.TRUE_FIELD)) {
				loginSuccsess = true;
			} else {
				answer = paramMap.get(MessegeParser.MsgFields.ERROR.name());
				setListnerResult(answer);
			}
			latch.countDown();
		}
		
	}
	
	
	@Override
	public void waitForComplete() {
		try {
			latch.await(10, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			//don't care
		}
		
	}


	public synchronized String getListnerResult() {
		return listnerResult;
	}


	public synchronized void setListnerResult(String listnerResult) {
		this.listnerResult = listnerResult;
	}


	public synchronized boolean isLoginSuccsess() {
		return loginSuccsess;
	}





}
